<template>
  <div style="padding: 40px">

    <el-table :data="tableData" border stripe style="width: 100%;" header-align="center" align="center">
      <el-table-column label="通知ID" width="70px"  header-align="center" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="activityName" label="活动名称" header-align="center" align="center"/>
      <el-table-column prop="startTime" label="开始时间" :formatter="formatMeetingTime" header-align="center" align="center"/>
      <el-table-column prop="endTime" label="结束时间"  :formatter="formatEndtingTime" header-align="center" align="center"/>
      <el-table-column prop="location" label="活动地点" header-align="center" align="center"/>
      <el-table-column prop="description" label="活动描述" header-align="center" align="center"/>
      <el-table-column prop="organizer" label="组织者姓名" header-align="center" align="center"/>
      <el-table-column fixed="right" label="操作" width="180" header-align="center" align="center"   v-if=" userRole === 'teacher'">
        <template #default="scope">
          <!-- 编辑按钮  -->
          <el-button v-if=" userRole === 'teacher'" type="primary" @click="handleEdit(scope.row)">更新</el-button>

          <!-- 删除按钮 -->
          <el-popconfirm  v-if=" userRole === 'teacher'" title="这段内容确定删除吗?" @onConfirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 点击编辑的弹窗 -->
    <el-dialog :visible.sync="dialogVisible1" title="更新完满活动" width="30%">

      <el-form :model="form" label-width="120px">
        <el-form-item label="活动名称">
          <el-input v-model="form.activityName" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
              v-model="form.startTime"
              type="datetime"
              placeholder="选择开始时间"    style="width: 50%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
              v-model="form.endTime"
              type="datetime"
              placeholder="选择结束时间"    style="width: 50%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="活动地点">
          <el-input v-model="form.location" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="活动描述">
          <el-input v-model="form.description" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="组织人姓名">
          <el-input v-model="form.organizer" style="width: 50%"></el-input>
        </el-form-item>
      </el-form>


      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取消</el-button>
        <el-button type="primary" @click="saveEdit">确定</el-button>
      </span>

    </el-dialog>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total,prev, pager, next, jumper"
        :total="total">
    </el-pagination>


  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  getGuanjiluCell, getGuanliyuanDeleteDaka, getZZZThAll
} from '@/http/userList.js'
import moment from "moment";

export default {
  name: "Notice",

  data() {
    return {
      form: {
        "activityName": "",
        "description": "",
        "endTime": "",
        "location": "",
        "organizer": "",
        "startTime": ""
      },

      dialogVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      currentPage: 1,
      pageSize: 10,
      total:0,
      tableData: [],
    }

  },
  created() {
    this.load()
  },

  methods: {
    formatMeetingTime(row) {
      return moment(row.startTime).format('YYYY-MM-DD HH:mm:ss'); // 根据需要调整格式
    },
    formatEndtingTime(row) {
      return moment(row.endTime).format('YYYY-MM-DD HH:mm:ss'); // 根据需要调整格式
    },


    async load() {
      const { code, message, data } = await getGuanjiluCell({ current: this.currentPage, size: this.pageSize})
      console.log(data)
      this.tableData = data.records
      this.total= Number(data.total)

    },
    handleEdit(row) {

      console.log(row);
      this.form = { ...row }
      this.dialogVisible1 = true
    },
    // 确认修改
    async saveEdit() {

      const { code, message } = await getZZZThAll(this.form)
      if (code == '0') {
        this.$message({
          type: "success",
          message: "更新成功"
        })
      } else {
        this.$message({
          type: "error",
          message: "更新失败"
        })
      }
      this.dialogVisible1 = false
      this.load()
    },
    async handleDelete(id) {
      console.log("id");
      const { code, message } = await getGuanliyuanDeleteDaka({ id: id })
      if (code == '0') {
        this.$message({
          type: "success",
          message: "删除成功"
        })
      } else {
        this.$message({
          type: "error",
          message: message
        })
      }
      this.load()  // 删除之后重新加载表格的数据
    },
    handleSizeChange(pageSize ) {
      this.pageSize = pageSize;
      this.currentPage = 1;
      this.load()
    },
    async handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      const { code, message, data } = await getGuanjiluCell({ current: pageNum, size: this.pageSize })
      console.log(data)
      this.tableData = data.records
      this.total= Number(data.total)
      this.load(); // 重新加载数据
    },
    onDaloag(row) {
      this.form1 = row
      this.dialogVisible2 = true
    }
  },
  computed: {
    ...mapGetters({
      userRole: 'user/getUserRole'
    })
  }

}
</script>

<style></style>
